package cn.wqb.cloudar.utils;

import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 需要System.load('I:/JavaWeb/opencv_java/opencv_java342.dll')
 * 需要依赖I:/JavaWeb/opencv_java/opencv-342.jar
 */

public class ARImageUtils {

    public static String getImageBase64FromHttpUrl(String url) {
        HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection) new URL(url).openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5 * 1000);
            InputStream inStream = conn.getInputStream();
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = inStream.read(buffer)) != -1) {
                outStream.write(buffer, 0, len);
            }
            inStream.close();
            byte[] data = outStream.toByteArray();
            BASE64Encoder encoder = new BASE64Encoder();
            String base64 = encoder.encode(data);
            return base64;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Mat base64ToMat(String base64) {
        try {
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] origin = decoder.decodeBuffer(base64);
            InputStream inputStream = new ByteArrayInputStream(origin);
            BufferedImage buffImage = ImageIO.read(inputStream);
            byte[] data = ((DataBufferByte) buffImage.getRaster().getDataBuffer()).getData();
            Size size = new Size(new double[]{buffImage.getHeight(), buffImage.getWidth()});
            Mat mat = new Mat(size, CvType.CV_8UC3);
            mat.put(0, 0, data);
            inputStream.close();
            return mat;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Mat bytesToMat(byte[] bytes) {
        int length = bytes.length;
        Mat res = new Mat(length, 1, CvType.CV_8UC1);
        byte[] buff = new byte[length];
        for (int i = 0; i < length; i++) {
            byte b = bytes[i];
            buff[i] = b;
        }
        res.put(0, 0, buff);
        return res;
    }

    public static byte[] matToBytes(Mat mat) {
        try {
            MatOfByte mob = new MatOfByte();
            Imgcodecs.imencode(".jpg", mat, mob);
            byte[] byteArray = mob.toArray();
            return byteArray;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
